package com.flink.windowFunctionDemo;

import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;

public class IncrementalAggregationExample {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStream<Integer> inputStream = env.fromElements(1, 2, 3, 4, 5);

        DataStream<Integer> resultStream = inputStream
                .timeWindowAll(Time.seconds(5))
                .reduce(new ReduceFunction<Integer>() {
                    @Override
                    public Integer reduce(Integer value1, Integer value2) throws Exception {
                        return value1 + value2;
                    }
                });

        resultStream.print();
        env.execute("Incremental Aggregation Example");
    }
}

/*
Flink 的窗口函数可以分为两大类：
增量聚合函数（Incremental Aggregation Functions）
介绍：每来一条数据就进行一次聚合计算，只保存中间结果，不会缓存所有数据，节省内存。
示例：ReduceFunction 和 AggregateFunction 都属于增量聚合函数。*/
